package controller;

import entity.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/profile")
public class profileController extends HttpServlet {
    private UserService us = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        User u = us.findById(id);
        if (u != null) {
            req.setAttribute("user", u);
            req.getRequestDispatcher("/WEB-INF/views/user/profile.jsp").forward(req, resp);

        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String name = req.getParameter("name");
        String gender = req.getParameter("gender");
        String permission = req.getParameter("permission");
        String remark = req.getParameter("remark");
        String id = req.getParameter("id");
        int num = us.update(username, name, gender, permission, remark, id);
        if (num != 0){
            resp.sendRedirect("/profile?id="+id);
        }else {
            resp.sendRedirect("/profile?id="+id);
        }
    }
}









